//
//  ShoppingManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-10.
//
//

#import "ShoppingManager.h"
#import "ServerCommunicationManager.h"
#import "ShoppingCategoryRequest.h"
#import "ShoppingCategoryResponse.h"
#import "ShoppingGoodsRequest.h"
#import "ShoppingGoodsResponse.h"
#import "ShoppingOrderRequest.h"
#import "ShoppingOrderResponse.h"
#import "recommendProductRequest.h"
#import "RecommendProductResponse.h"

@implementation ShoppingManager
static ShoppingManager* gManager = nil;


+ (ShoppingManager*)shareManager
{
    if (gManager == nil) {
        gManager = [[ShoppingManager alloc] init];
    }
    return gManager;
}

- (void)getShoppingCategory
{
    ShoppingCategoryRequest *request = [[ShoppingCategoryRequest alloc]initWithDelegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)getCategoryGoods:(NSString*)categoryId
{
    ShoppingGoodsRequest *request = [[ShoppingGoodsRequest alloc]initWithCategoryId:categoryId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)submitOrders:(NSMutableArray*)orders tel:(NSString*)telNum
{
    ShoppingOrderRequest *request = [[ShoppingOrderRequest alloc] initWithOrderArray:orders Tel:telNum Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)getRecommends
{
    RecommendProductRequest *request = [[RecommendProductRequest alloc] initWithDelegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
    [request release];
}

- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[ShoppingCategoryResponse class]])
    {
        [self handleShoppingCategorySuccess:(ShoppingCategoryResponse*)response];
    }
    else if([response isKindOfClass:[ShoppingGoodsResponse class]])
    {
        [self handleCategoryGoodsSuccess:(ShoppingGoodsResponse*)response];
    }
    else if([response isKindOfClass:[ShoppingOrderResponse class]])
    {
        [self handleSubmitOrdersSuccess:(ShoppingOrderResponse *)response];
    }
    else if([response isKindOfClass:[RecommendProductResponse class]])
    {
        [self handleRecommandProductSuccess:(RecommendProductResponse *)response];
    }
}

- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[ShoppingCategoryResponse class]])
    {
        [self.delegate onGetShoppingCategoryFinish:NO Categorys:nil];
    }
    else if ([response isKindOfClass:[ShoppingGoodsResponse class]])
    {
        [self.delegate onGetCategoryGoods:NO GoodsArray:nil];
    }
    
    
}

- (void)handleShoppingCategorySuccess:(ShoppingCategoryResponse*)response
{
    NSMutableArray *categorys = response.categoryArray;
    if (categorys)
    {
        [self.delegate onGetShoppingCategoryFinish:YES Categorys:categorys];
    }
    else
    {
        [self.delegate onGetShoppingCategoryFinish:NO Categorys:nil];
    }
}

- (void)handleCategoryGoodsSuccess:(ShoppingGoodsResponse*)response
{
    NSMutableArray *goodsArray = response.goodsArray;
    if (goodsArray)
    {
        [self.delegate onGetCategoryGoods:YES GoodsArray:goodsArray];
    }
    else
    {
        [self.delegate onGetCategoryGoods:NO GoodsArray:nil];
    }
}

- (void)handleSubmitOrdersSuccess:(ShoppingOrderResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate onSubmitOrders:YES Tn:tn];
    }
    else
    {
        [self.delegate onSubmitOrders:NO Tn:nil];
    }
}

- (void)handleRecommandProductSuccess:(RecommendProductResponse*)response
{
    NSMutableArray *merchantArray = response.merchantArray;
    NSMutableArray *goodsArray = response.goodsArray;
    
    [self.delegate onGetRecommendProducts:YES GoodsArray:goodsArray MerchantArray:merchantArray];
    
}

@end
